package com.zhang.service;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.zhang.entity.ChannelBody;
import com.zhang.entity.CommonTemplate;
import com.zhang.entity.Node;
import com.zhang.entity.ScriptPart;
import com.zhang.entity.co.MachineResult;
import com.zhang.entity.vo.NettyClientMessage;
import com.zhang.exception.ServiceException;
import com.zhang.util.DeviceOperateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Map;

import static com.zhang.common.Constant.*;

/**
 * User: 张
 * Date: 2024-11-15
 * Time: 21:41
 * Description:
 */
@Component
public class AsyncService {

    private final DeviceOperateUtil deviceOperateUtil;
    public static Integer flag = 0;
    @Autowired
    public AsyncService(DeviceOperateUtil deviceOperateUtil) {
        this.deviceOperateUtil = deviceOperateUtil;
    }


    @Async
    public void executeScript(String JsonStr) {
        CommonTemplate commonTemplate = JSON.parseObject(JsonStr, CommonTemplate.class);
        Gson gson = new Gson();
        Node tree = gson.fromJson(commonTemplate.getScript(), Node.class);

        for (int i = 0; i < commonTemplate.getExecuteFrequency(); i++) {
            reduceTree(tree,commonTemplate.getExecuteId());
            if (flag == 1) {
                AsyncService.flag = 0;
                break;
            }
        }
        IoTNettyClient.sendMessageToServer(JSON.toJSONString(new ChannelBody(DEVICE_RESULT, JSON.toJSONString(new NettyClientMessage(SUCCESS, commonTemplate.getExecuteId(), "成功")))));

    }

    public void reduceTree(Node node,String executeId) {
        // todo 递归执行脚本
        String result = "";
        try {
            result = deviceOperateUtil.DeviceOperate(node.getMethod(),node.getAction(), node.getParameter());
        }catch (Exception e){
            IoTNettyClient.sendMessageToServer(JSON.toJSONString(new ChannelBody(DEVICE_RESULT, JSON.toJSONString(new NettyClientMessage(FAIL, executeId, e.getMessage())))));
            throw new ServiceException("脚本执失败");
        }

        // todo 解析返回数据
        MachineResult machineResult = null;
        try {
            machineResult = JSON.parseObject(result, MachineResult.class);
        }catch (Exception e){
            IoTNettyClient.sendMessageToServer(JSON.toJSONString(new ChannelBody(DEVICE_RESULT, JSON.toJSONString(new NettyClientMessage(FAIL, executeId, e.getMessage())))));
            throw new ServiceException("脚本返回数据格式异常");
        }

        // todo 判断条件
        if (node.getChildren().size() == 1){
            reduceTree(node.getChildren().get(0),executeId);
        }
        if (node.getChildren().size() > 1){
            for (Node child : node.getChildren()) {
                if (child.getDirection().equalsIgnoreCase(machineResult.getData())){
                    reduceTree(child,executeId);
                    break;
                }
            }
        }
    }
}
